﻿using System.ServiceModel;
using System.Web.Security;

namespace VASJ.Provider
{
    public class AcpInetMembershipProvider : MembershipProvider
    {
        BasicHttpBinding _binding;
        EndpointAddress _endpoint;

        string _ApplicationName = string.Empty;

        string username = string.Empty;
        string password = string.Empty;

        /// <summary>
        /// Init
        /// </summary>
        public AcpInetMembershipProvider()
        {
            BaseHelper.GetConfig(out _binding, out _endpoint, out _ApplicationName, out username, out password);
        }

        public override string ApplicationName
        {
            get
            {
                return this._ApplicationName;
            }
            set
            {
                this._ApplicationName = value;
            }
        }

        /// <summary>
        /// ChangePassword
        /// </summary>
        /// <param name="username"></param>
        /// <param name="oldPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.ChangePassword(username, oldPassword, newPassword);
        }

        /// <summary>
        /// ChangePasswordQuestionAndAnswer
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="newPasswordQuestion"></param>
        /// <param name="newPasswordAnswer"></param>
        /// <returns></returns>
        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer);
        }

        /// <summary>
        /// CreateUser
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="email"></param>
        /// <param name="passwordQuestion"></param>
        /// <param name="passwordAnswer"></param>
        /// <param name="isApproved"></param>
        /// <param name="providerUserKey"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.CreateUser(out status, this._ApplicationName, username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey);
        }

        /// <summary>
        /// DeleteUser
        /// </summary>
        /// <param name="username"></param>
        /// <param name="deleteAllRelatedData"></param>
        /// <returns></returns>
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.DeleteUser(this._ApplicationName, username, deleteAllRelatedData);
        }

        public override bool EnablePasswordReset
        {
            get
            {
                AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
                acpiNet.ClientCredentials.UserName.UserName = this.username;
                acpiNet.ClientCredentials.UserName.Password = this.password;

                return acpiNet.GetEnablePasswordReset();
            }
        }

        public override bool EnablePasswordRetrieval
        {
            get
            {
                AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
                acpiNet.ClientCredentials.UserName.UserName = this.username;
                acpiNet.ClientCredentials.UserName.Password = this.password;

                return acpiNet.GetEnablePasswordRetrieval();
            }
        }

        /// <summary>
        /// FindUsersByEmail
        /// </summary>
        /// <param name="emailToMatch"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            MembershipUser[] lst = acpiNet.FindUsersByEmail(out totalRecords, this._ApplicationName, emailToMatch, pageIndex, pageSize);

            MembershipUserCollection retVal = new MembershipUserCollection();

            for (int i = 0; i < lst.Length; i++)
            {
                retVal.Add(lst[i]);
            }

            return retVal;
        }

        /// <summary>
        /// FindUsersByName
        /// </summary>
        /// <param name="usernameToMatch"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            MembershipUser[] lst = acpiNet.FindUsersByName(out totalRecords, this._ApplicationName, usernameToMatch, pageIndex, pageSize);

            MembershipUserCollection retVal = new MembershipUserCollection();

            for (int i = 0; i < lst.Length; i++)
            {
                retVal.Add(lst[i]);
            }

            return retVal;
        }

        /// <summary>
        /// GetAllUsers
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            MembershipUserCollection mc = new MembershipUserCollection();

            MembershipUser[] mu = acpiNet.GetAllUsers(out totalRecords, this._ApplicationName, pageIndex, pageSize);

            for (int i = 0; i < mu.Length; i++)
            {
                mc.Add(mu[i]);
            }

            return mc;
        }

        /// <summary>
        /// GetNumberOfUsersOnline
        /// </summary>
        /// <returns></returns>
        public override int GetNumberOfUsersOnline()
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetNumberOfUsersOnline();
        }

        /// <summary>
        /// GetPassword
        /// </summary>
        /// <param name="username"></param>
        /// <param name="answer"></param>
        /// <returns></returns>
        public override string GetPassword(string username, string answer)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetPassword(username, answer);
        }

        /// <summary>
        /// GetUser
        /// </summary>
        /// <param name="username"></param>
        /// <param name="userIsOnline"></param>
        /// <returns></returns>
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;            

            return acpiNet.GetUser(this._ApplicationName, username, userIsOnline);
        }

        /// <summary>
        /// GetUser
        /// </summary>
        /// <param name="providerUserKey"></param>
        /// <param name="userIsOnline"></param>
        /// <returns></returns>
        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetUserByProviderUserKey(this._ApplicationName, providerUserKey, userIsOnline);
        }

        /// <summary>
        /// GetUserNameByEmail
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public override string GetUserNameByEmail(string email)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.GetUserNameByEmail(this._ApplicationName, email);
        }

        /// <summary>
        /// MaxInvalidPasswordAttempts
        /// </summary>
        public override int MaxInvalidPasswordAttempts
        {
            get
            {
                AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
                acpiNet.ClientCredentials.UserName.UserName = this.username;
                acpiNet.ClientCredentials.UserName.Password = this.password;

                return acpiNet.GetMaxInvalidPasswordAttempts();
            }
        }

        /// <summary>
        /// MinRequiredNonAlphanumericCharacters
        /// </summary>
        public override int MinRequiredNonAlphanumericCharacters
        {
            get
            {
                AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
                acpiNet.ClientCredentials.UserName.UserName = this.username;
                acpiNet.ClientCredentials.UserName.Password = this.password;

                return acpiNet.GetMinRequiredNonAlphanumericCharacters();
            }
        }

        /// <summary>
        /// MinRequiredPasswordLength
        /// </summary>
        public override int MinRequiredPasswordLength
        {
            get
            {
                AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
                acpiNet.ClientCredentials.UserName.UserName = this.username;
                acpiNet.ClientCredentials.UserName.Password = this.password;

                return acpiNet.GetMinRequiredPasswordLength();
            }
        }

        /// <summary>
        /// PasswordAttemptWindow
        /// </summary>
        public override int PasswordAttemptWindow
        {
            get
            {
                AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
                acpiNet.ClientCredentials.UserName.UserName = this.username;
                acpiNet.ClientCredentials.UserName.Password = this.password;

                return acpiNet.GetPasswordAttemptWindow();
            }
        }

        /// <summary>
        /// PasswordFormat
        /// </summary>
        public override MembershipPasswordFormat PasswordFormat
        {
            get
            {
                AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
                acpiNet.ClientCredentials.UserName.UserName = this.username;
                acpiNet.ClientCredentials.UserName.Password = this.password;

                return acpiNet.GetPasswordFormat();
            }
        }

        /// <summary>
        /// PasswordStrengthRegularExpression
        /// </summary>
        public override string PasswordStrengthRegularExpression
        {
            get
            {
                AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
                acpiNet.ClientCredentials.UserName.UserName = this.username;
                acpiNet.ClientCredentials.UserName.Password = this.password;

                return acpiNet.GetPasswordStrengthRegularExpression();
            }
        }

        /// <summary>
        /// RequiresQuestionAndAnswer
        /// </summary>
        public override bool RequiresQuestionAndAnswer
        {
            get
            {
                AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
                acpiNet.ClientCredentials.UserName.UserName = this.username;
                acpiNet.ClientCredentials.UserName.Password = this.password;

                return acpiNet.GetRequiresQuestionAndAnswer();
            }
        }

        /// <summary>
        /// RequiresUniqueEmail
        /// </summary>
        public override bool RequiresUniqueEmail
        {
            get
            {
                AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
                acpiNet.ClientCredentials.UserName.UserName = this.username;
                acpiNet.ClientCredentials.UserName.Password = this.password;

                return acpiNet.GetRequiresUniqueEmail();
            }
        }

        /// <summary>
        /// ResetPassword
        /// </summary>
        /// <param name="username"></param>
        /// <param name="answer"></param>
        /// <returns></returns>
        public override string ResetPassword(string username, string answer)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.ResetPassword(username, answer);
        }

        /// <summary>
        /// UnlockUser
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public override bool UnlockUser(string userName)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet.UnlockUser(userName);
        }

        /// <summary>
        /// UpdateUser
        /// </summary>
        /// <param name="user"></param>
        public override void UpdateUser(MembershipUser user)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            acpiNet.UpdateUser(user);
        }

        /// <summary>
        /// ValidateUser
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public override bool ValidateUser(string username, string password)
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            if (this._ApplicationName != "AcpInet")
            {
                return acpiNet.ValidateUser(username, password) && acpiNet.ValidateUserOnProject(this._ApplicationName, username);
            }
            else return acpiNet.ValidateUser(username, password);
        }
    }
}